package com.example.gulimall.order.config;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradeCloseResponse;
import com.example.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118634358";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDHQ7bq1y/LnuoyJ4i+lhP3k18DNxxD+qkzGY1M7c3r02AHbBDPjbuUyZZTFt78zIc8pS6YBq4/F7yehSnRKl1/iqXFXmwhp58GDqYAGtr8hmgG6aSmRY9eP1gPvk1zpv7sGbPClS7B90bvai8HtzYPjASualyBdApGscwyM9MQJFplWzfIFeUD+Ef7DbfnSoxd16ToOdDixjLvLNxQ1vTaZqTfR9Kr6BfdGHIYtgHVHNnVyRWm+VZzbTSHrqgRqDFc2MXR3BQdhvZ7C1NkHszpmRuzaWIMAMfZYgOAXeX4mcbXk5lAVybYnJeL/N+SPzIkQ8mjT7F81b/ua/nek4T5AgMBAAECggEAWf7UO0ZK2N0CCvreAK3JgNR3Yf1e77ji+Cg/MBJrIWpLAa8l/IeXw8Az+3zl4ykAnzw/SfwicG5pCvM7irNmnPbinbxie+S3psWw5XU7mDef/hdB/LKH6NoVDK83TdeLZNjK3kQAZSwoRnMfoI13hhgH+ToiY3kkNv+QKNBNMAleNMwUfIvWUHaFgM3j7/zDBx3Nlpr22nmALNTgReSQAxKEgJttrKGvzRRnDXay0WMgEaawfiO/TmBMOBUQw1DreGVS+sowPvEP7oSdIySAq/axs1mO5otkJPRCUrNYeM88MBDP58SCbAkE9cpUyaSkBfh4sVmCbeCT/lvEsoZeHQKBgQD4dQyF4vCgJKS+dxm9+0qQSCoXZRxmMXs/qv2reyC/bPRlgfetzGjCKOxd6Iz1mj4Yfnx6fcpYaJw1PPb3VJ4Pk9X1x+QAOAWe2I/i3EhzxhSdsYR0YqS1HnvQ+3XfDKHXwcFMchpdjK4gorI7MqxXnSLQkUM8yScMWXW66zn4+wKBgQDNUFn2RuA1XLqOFEqzkFUZSw+H0VBYroE3fH9FOOzYSk0RxCZKFIwAcc6eBNBiklQm6oYK30AWY1DXOggBe009rxeGC9gr5Wy7pWIt0s0AE5mvXdSLjA31aDzf56MKpeojjkZ5DG/h8zT0El8b779u4vERwZGJ/gLooFrH3Fw/mwKBgDhkgMomv0zp5Eaiy8H2Nm9XIc6bkX2Uf4/s5JS2ZrFsLnCXtjrJaYGgPj32WYApWSzAEMrQoEu+uq3Ov5NVrmaEvIyDqTlvfb8XElmAfCVi8Sz1rMWp2g6N87Z0TMWqnVVWUeRjBZ70HlWj9+Y90yYzsUI7plI2qdlAuNsLJuZfAoGAL6PwbAQALIGqzCMLPmVESc0v+lzoXtuHIj/iLEbMNlfvUCsKk2/3GHdP8Ty+udCodCtBFS6+aTN8Y6SUjPt2bdS+CZ2f+HeQ5m22vVlHtjfWFmoYUpUZ/BLG5VGTdHoRPVTbJ2VqZKyRYf0NUqcr7Wf9dhhLVBC7zgy/Y9bPFPcCgYEAmN00idfDwCIRzr6kUZV6EdsGq3b8SUZDa+9HHK5siLPq4MMj32wzwWct62vuQ/o0zk+fqISnxniURdZTOdX4bd/+p1EjFavx9EEvCX1GClCCmNLEEe0xxr5z4lW+5OttZ2xPf2JOJkxmv/63kMjWIRr91pyeg48sgGUaN46fJw4=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlIsjglRFum6Ce7sm2JyNW2LvQHKOYAEHLE1c13uu9PdpNbUFsYq9bkuvOJDvQFvePZC7q2UfEmcW2gxX4+/amnUUaZg2IFFuqw78PS70ClUv9CkQ6LOET2wjKSyo6T9EY5E+GpIc3u0wy0W5isfFqKwDXdj5juUgP0f7VK2pIjQ/7uWhLCyajIuLMnBg4EOUAg/TdbmuMxP88qL+w1RtzcsUxEHUMYkwKzqF1a4tw53RlOcZxMhl+ApIgPX3ENYQ5wYZI9/0y0Yb9eHoZ/aysYyLB1Gwez7vnv8u5k9lUl8aKau8vdmOcPPpQiWIz9+dXSFGE+Xfo1TFgeHQ3LlEjQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://vndyta.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        //System.out.println("支付宝的响应："+result);

        return result;

    }

    public void close(PayVo vo) throws AlipayApiException{
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2");
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("trade_no", vo.getOut_trade_no());
        request.setBizContent(bizContent.toString());
        AlipayTradeCloseResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }
}
